﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using ParserGenerator.Collections;

namespace ParserGenerator.GrammarConstruction
{
    [Serializable]
    public sealed class GrammarNonterminalSymbol : GrammarSymbol
    {
        public new bool Nullable { get; internal set; }
        public ReadOnlySet<GrammarTerminalSymbol> FirstSet { get; internal set; }
        public ReadOnlySet<GrammarTerminalSymbol> FollowSet { get; internal set; }
        public ReadOnlyCollection<GrammarProduction> Derives { get; internal set; }

        [NonSerialized]
        private ReadOnlyCollection<GrammarProduction> _productionsWrapper;
        private readonly List<GrammarProduction> _productions;
        public ReadOnlyCollection<GrammarProduction> Productions
        {
            get
            {
                if (_productionsWrapper == null)
                    _productionsWrapper = _productions.AsReadOnly();
                return _productionsWrapper;
            }
        }

        internal GrammarNonterminalSymbol()
        {
            _productions = new List<GrammarProduction>();
        }

        internal GrammarProduction DefineProduction()
        {
            GrammarProduction prod = new GrammarProduction();
            prod.Owner = this;
            _productions.Add(prod);
            return prod;
        }
    }
}
